//
//  GoodDetailBottomView.m
//  B2C
//
//  Created by xiaowu on 2017/8/1.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodDetailBottomView.h"

@interface GoodDetailBottomView ()

@end

@implementation GoodDetailBottomView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.view = [[[NSBundle mainBundle] loadNibNamed:@"GoodDetailBottomView" owner:self options:nil] firstObject];
        self.view.frame = self.bounds;
        [self addSubview:self.view];
        
        [self configureUI];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"GoodDetailBottomView" owner:self options:nil] firstObject];
    self.view.frame = self.bounds;
    [self addSubview:self.view];
    
    [self configureUI];
}


- (void)configureUI {
    self.cartImageView.image = [UIImage imageNamed:@"good_cart"];
    self.cartLabel.text = @"购物车";
    
    self.cartLabel.font = TextNormalFontSize(10);
    self.cartLabel.textColor = [UIColor colorWithHex:0x747474 alpha:1.0];
    self.collectLabel.font = TextNormalFontSize(10);
    self.collectLabel.textColor = [UIColor colorWithHex:0x747474 alpha:1.0];
    self.cartCountLabel.textColor = [UIColor whiteColor];
    self.cartCountLabel.layer.cornerRadius = self.cartCountLabel.height * 0.5;
    self.cartCountLabel.clipsToBounds = YES;
    self.cartCountLabel.backgroundColor = [UIColor orangeColor];
    self.cartCountLabel.font = TextNormalFontSize(12);
    
    [self.addCartButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:0xfcc22f alpha:1.0]] forState:UIControlStateNormal];
    [self.addCartButton setTitle:@"加入购物车" forState:UIControlStateNormal];
    [self.addCartButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.addCartButton.titleLabel.font = TextNormalFont14;
    
    [self.buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.buyButton setBackgroundImage:[UIImage imageWithColor:[UIColor orangePriceColor]] forState:UIControlStateNormal];
    self.buyButton.titleLabel.font = TextNormalFont14;
}

- (void)setBottomValueWithCollect:(BOOL)isCollect andCartCount:(NSString *)cartCount {
    if (isCollect) {
        self.collectLabel.text = @"取消收藏";
        self.collectImageView.image = [UIImage imageNamed:@"good_collection-c"];
    } else {
        self.collectLabel.text = @"收藏";
        self.collectImageView.image = [UIImage imageNamed:@"good_collection"];
    }
    
    NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont12};
    CGFloat width = [cartCount boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    
    self.cartCountLabel.width = width;
    if (self.cartCountLabel.width < self.cartCountLabel.height) {
        self.cartCountLabel.width = self.cartCountLabel.height;
    }
    
    if (cartCount.integerValue > 0) {
        self.cartCountLabel.text = cartCount;
        self.cartCountLabel.hidden = NO;
    } else {
        self.cartCountLabel.text = @"";
        self.cartCountLabel.hidden = YES;
    }
    [(AppDelegate *)[UIApplication sharedApplication].delegate updateBadgeValue:cartCount.integerValue];
}

#pragma mark - Action
- (IBAction)collectionButtonClick:(id)sender {
    if (self.block) {
        self.block(0);
    }
}
- (IBAction)cartButtonClick:(id)sender {
    if (self.block) {
        self.block(1);
    }
}
- (IBAction)addCartButtonClick:(id)sender {
    if (self.block) {
        self.block(2);
    }
}
- (IBAction)buyButtonClick:(id)sender {
    if (self.block) {
        self.block(3);
    }
}


@end
